﻿
Imports osi.root.constants
Imports osi.root.formation
Imports osi.root.connector
Imports osi.root.procedure
Imports osi.root.utils
Imports osi.root.lock

Partial Public Class pre_generated_device_pool(Of T)
    Inherits device_pool(Of T)
    Implements new_device_event_isync_device_pool(Of T)

    Public Event new_device_inserted() _
                 Implements new_device_event_isync_device_pool(Of T).new_device_inserted
    Private ReadOnly q As qless2(Of T)
    Private ReadOnly new_device_inserted_action As Action(Of T)

    Public Sub New(ByVal max_count As UInt32,
                   ByVal identity As String,
                   Optional ByVal ctor As Func(Of pointer(Of T), event_comb) = Nothing,
                   Optional ByVal validator As Func(Of T, Boolean) = Nothing,
                   Optional ByVal closer As Action(Of T) = Nothing,
                   Optional ByVal device_id As Func(Of T, String) = Nothing,
                   Optional ByVal new_device_inserted_action As Action(Of T) = Nothing)
        MyBase.New(max_count, identity, ctor, validator, closer, device_id)
        Me.q = New qless2(Of T)()
        Me.new_device_inserted_action = new_device_inserted_action
    End Sub

    Public Sub New(ByVal max_count As UInt32,
                   Optional ByVal ctor As Func(Of pointer(Of T), event_comb) = Nothing,
                   Optional ByVal validator As Func(Of T, Boolean) = Nothing,
                   Optional ByVal closer As Action(Of T) = Nothing,
                   Optional ByVal device_id As Func(Of T, String) = Nothing,
                   Optional ByVal new_device_inserted_action As Action(Of T) = Nothing)
        MyBase.New(max_count, ctor, validator, closer, device_id)
        Me.q = New qless2(Of T)()
        Me.new_device_inserted_action = new_device_inserted_action
    End Sub

    Public Sub New(ByVal identity As String,
                   Optional ByVal ctor As Func(Of pointer(Of T), event_comb) = Nothing,
                   Optional ByVal validator As Func(Of T, Boolean) = Nothing,
                   Optional ByVal closer As Action(Of T) = Nothing,
                   Optional ByVal device_id As Func(Of T, String) = Nothing,
                   Optional ByVal new_device_inserted_action As Action(Of T) = Nothing)
        MyBase.New(identity, ctor, validator, closer, device_id)
        Me.q = New qless2(Of T)()
        Me.new_device_inserted_action = new_device_inserted_action
    End Sub

    Public Sub New(Optional ByVal ctor As Func(Of pointer(Of T), event_comb) = Nothing,
                   Optional ByVal validator As Func(Of T, Boolean) = Nothing,
                   Optional ByVal closer As Action(Of T) = Nothing,
                   Optional ByVal device_id As Func(Of T, String) = Nothing,
                   Optional ByVal new_device_inserted_action As Action(Of T) = Nothing)
        MyBase.New(ctor, validator, closer, device_id)
        Me.q = New qless2(Of T)()
        Me.new_device_inserted_action = new_device_inserted_action
    End Sub

    Protected NotOverridable Overrides Function get_device(r As pointer(Of T)) As event_comb
        assert(False)
        Return Nothing
    End Function

    Protected NotOverridable Overrides Function sync_get_device(ByRef r As T) As Boolean
        Return q.pop(r)
    End Function

    Protected NotOverridable Overrides Function release_device(ByVal c As T) As Boolean
        q.push(c)
        Return True
    End Function

    Public Shadows Function sync_get(ByRef r As T) As Boolean _
                                    Implements new_device_event_isync_device_pool(Of T).sync_get
        Return MyBase.sync_get(r)
    End Function

    Public Shadows Function release(ByVal c As T) As Boolean _
                                   Implements new_device_event_isync_device_pool(Of T).release
        Return MyBase.release(c)
    End Function

    Protected NotOverridable Overrides Function get_free_count() As UInt32
        Return q.size()
    End Function

    Private Function insert_new_device(ByVal c As T) As Boolean
        If Not expired() AndAlso
           is_valid_device(c) Then
            If increase_total_count() Then
                q.push(c)
                assert(Not limited_max_count() OrElse q.size() <= max_count())
                RaiseEvent new_device_inserted()
                If Not new_device_inserted_action Is Nothing Then
                    new_device_inserted_action(c)
                End If
                Return True
            Else
                Return False
            End If
        Else
            Return False
        End If
    End Function

    Protected Function insert_new_device(ByVal c As T, ByVal close_if_fail As Boolean) As Boolean
        Dim r As Boolean = False
        r = insert_new_device(c)
        If r Then
            Return True
        Else
            If close_if_fail Then
                begin_close_device(c)
            End If
            Return False
        End If
    End Function

    Protected NotOverridable Overrides Sub close_devices()
        close_existing_devices(q)
    End Sub

    Protected Overrides Sub Finalize()
        GC.KeepAlive(q)
        MyBase.Finalize()
    End Sub
End Class
